import { login, getUserInfo, getUserDetail } from '@/api/user'
import { setToken, getToken, removeToken, setTime } from '@/utils/auth'
import { resetRouter } from '@/router'
const state = {
  token: getToken() || '',
  userInfo: {}// 用户基本信息
}
const mutations = {
  // 设置token
  settoken(state, data) {
    state.token = data
    // 保存token
    setToken(data)
  },
  // 删除token
  removetoken(state) {
    state.token = ''
    removeToken()
  },
  setUserInfo(state, data) {
    state.userInfo = data
    console.log(state.userInfo)
  },
  removeUserInfo(state) {
    state.userInfo = {}
  }

}
const actions = {
  // 登录接口
  async login(context, fromdata) {
    const res = await login(fromdata)
    // console.log(res)
    context.commit('settoken', res)
    setTime()
  },
  // 用户基本信息
  async getUserInfo(context) {
    const res = await getUserInfo()
    // 员工基本信息
    const detail = await getUserDetail(res.userId)
    console.log(res)
    context.commit('setUserInfo', { ...res, ...detail })
  },
  // 退出功能的实现
  logout(context) {
    // 清空路由权限
    resetRouter()
    context.commit('removetoken')
    context.commit('removeUserInfo')
  }

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
